package biz.datalk.industrialland.admin.component;

import biz.datalk.industrialland.admin.modules.system.service.ISysConfigService;
import biz.datalk.industrialland.common.config.JwtCryptoCfg;
import biz.datalk.industrialland.common.util.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Locale;

/**
 * @author tarofang@163.com
 * @date 2020-04-13
 */
@Slf4j
@Component
@ConditionalOnProperty(name = {"datalk.core.init.runner.enable"}, havingValue = "true", matchIfMissing = true)
public class DatalkCoreInitRunner implements ApplicationRunner {

    // private IDynformCacheService dynformCacheService;

    private final ISysConfigService sysConfigService;

    @Autowired
    public DatalkCoreInitRunner(ISysConfigService sysConfigService) {
        this.sysConfigService = sysConfigService;
    }

    // @Autowired
    // public void setDynformCacheService(IDynformCacheService dynformCacheService) {
    //     this.dynformCacheService = dynformCacheService;
    // }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        sysConfigService.refresh();
        // dynformCacheService.refresh();

        JwtCryptoCfg.getInstance().init(SpringContextUtils.getApplicationContext().getEnvironment());
        LocaleContextHolder.setDefaultLocale(Locale.CHINESE); // 设置Spring环境下全局默认的Locale配置

        checkUploadStorageIp();

        // Boolean extractValidCarInfoEnabled = SpringContextUtils.getProperty("extract_valid_car_info_enabled", Boolean.class, Boolean.FALSE);
        // if (extractValidCarInfoEnabled) {
        // }
    }

    private void checkUploadStorageIp() {
        Boolean uploadEnabled = SpringContextUtils.getProperty("upload.enabled", Boolean.class, Boolean.FALSE);
        if (!uploadEnabled) {
            return;
        }
        String uploadStorageType = SpringContextUtils.getProperty("upload.storage-type", String.class, StringUtils.EMPTY);
        if (!StringUtils.equals(uploadStorageType, "local")) {
            return;
        }
        String uploadLocalStorageIp = SpringContextUtils.getProperty("upload.local.storage-ip", String.class, StringUtils.EMPTY);
        Assert.isTrue(StringUtils.isNotBlank(uploadLocalStorageIp), "The 'upload.local.storage-ip' must not be blank.");
        Assert.isTrue(!StringUtils.equals(uploadLocalStorageIp, "127.0.0.1"), "The 'upload.local.storage-ip' must not be '127.0.0.1'.");
        Assert.isTrue(!StringUtils.equals(uploadLocalStorageIp, "0.0.0.0"), "The 'upload.local.storage-ip' must not be '0.0.0.0'.");
        log.info("The 'upload.local.storage-ip' is {}", uploadLocalStorageIp);
    }
}
